from pyvision.helpler.IDs import get_gid, get_gid_by_df
from pyvision.core.Props import DataSetProps, DatePickerProps, TableColumnProps, TableProps, SelectProps
import pandas as pd
from pandas.api.types import is_datetime64_dtype, is_numeric_dtype, is_string_dtype
from pyvision.core.Jsonable import Jsonable
from pyvision.core.DataModel import ComponentModel
from pyvision.core.Enums import ComponentModelTagEnum
from pyvision.wrappers.Components import DatePicker, Select, Table


class DataSet(Jsonable):

    def __init__(self,  df: pd.DataFrame, id: str = '') -> None:
        self.id = id or get_gid_by_df(df)
        self.data = df

    def new_table(self, id: str = ''):
        id = id or get_gid()
        tab = Table(id, self)
        col_mapping = {
            col: col
            for col in self.data.columns
        }
        tab.set_column_name(**col_mapping)
        return tab

    def new_slicer(self, column_name: str, id: str = ''):
        id = id or get_gid()

        # todo:根据列类型选择不同的控件作为切片器
        if is_datetime64_dtype(self.data[column_name]):
            return DatePicker(id, self, column_name)

        # if is_string_dtype(self.data[column_name]):
        #     pass
        return Select(id, self, column_name)

    def _2json(self):
        return {
            self.id: self.data
        }


class DataSetMapping(dict):

    def __init__(self) -> None:
        super().__init__()

    def to_json_dict(self):

        return {
            k: v.data
            for k, v in self.items()
        }
